var wk = {
    //成功弹出层
    success: function (message, url, ico) {
        var ico = ico ? ico : 1;//设置参数的默认值为1
        var url = url ? url : '';
        layer.msg(message, {icon: ico, time: 1500, shade: 0.1}, function (index) {
            layer.close(index);
            if (url != "") {
                window.location.href = url;
            }
        });
    },

    // 错误弹出层
    error: function (message, url, ico) {
        var ico = ico ? ico : 2;//设置参数的默认值为2
        var url = url ? url : '';
        layer.msg(message, {icon: ico, time: 1500, shade: 0.1}, function (index) {
            layer.close(index);
            if (url != "") {
                window.location.href = url;
            }
        });
    },

    // 确认弹出层
    confirm: function (id, url, msg) {
        var msg = msg ? msg : '确认删除此条记录吗?';
        layer.confirm(msg, {icon: 3, title: '提示'}, function (index) {
            $.ajax({
                url: url,
                type: "post",
                dataType: "json",
                data: "id=" + id,
                success: function (res) {
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1, time: 1500, shade: 0.1}, function () {
                            //重载当前页表格
                            $(".layui-laypage-btn").click();
                        });
                    } else if (res.code == 100) {
                        layer.msg(res.msg, {icon: 2, time: 1500, shade: 0.1});
                    }
                    // else if(res.code == 0){
                    //     layer.msg(res.msg,{icon:7,time:1500,shade: 0.1});
                    // }
                }
            });
            layer.close(index);
        })
    },
    //状态
    status: function (id, num, url, obj) {
        layui.use('form', function () {
            var form = layui.form;
            $.ajax({
                url: url,
                type: "post",
                dataType: "json",
                data: "id=" + id + '&num=' + num,
                success: function (res) {
                    var e = obj.elem.checked;
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1, time: 1500, shade: 0.1});
                        return false;
                    } else if (res.code == 100) {
                        // if("undefined" != typeof(res.type)){
                        //     $('#zt' + id ).next().addClass('layui-form-onswitch');
                        //     $('#zt' + id ).next().children("em").html('ON');
                        // }else{
                        //     if($('#zh' + id ).val()==2){
                        //         $('#zt' + id ).next().removeClass('layui-form-onswitch');
                        //         $('#zt' + id ).next().children("em").html('OFF');
                        //     }else{
                        //         $('#zt' + id ).next().addClass('layui-form-onswitch');
                        //         $('#zt' + id ).next().children("em").html('ON');
                        //     }
                        // }
                        obj.elem.checked = !e;
                        form.render();
                        layer.msg(res.msg, {icon: 2, time: 1500, shade: 0.1});
                        return false;
                    } else if (res.code == 0) {
                        obj.elem.checked = !e;
                        form.render();
                        // if($('#zh' + id ).val()==2){
                        //     $('#zt' + id ).next().removeClass('layui-form-onswitch');
                        //     $('#zt' + id ).next().children("em").html('OFF');
                        // }else{
                        //     $('#zt' + id ).next().addClass('layui-form-onswitch');
                        //     $('#zt' + id ).next().children("em").html('ON');
                        // }
                        // layer.msg(res.msg, {icon: 7, time: 1500, shade: 0.1});
                        // return false;
                    }
                }
            });
        });
    },
    //批量删除
    batchDel: function (ids, url) {
        layer.confirm('确认批量删除记录吗?', {icon: 3, title: '提示'}, function (index) {
            $.ajax({
                url: url,
                type: "post",
                dataType: "json",
                data: 'ids=' + ids,
                success: function (res) {
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1, time: 1500, shade: 0.1}, function () {
                            //重载当前页表格
                            $(".layui-laypage-btn").click();
                        });
                    } else if (res.code == 100) {
                        layer.msg(res.msg, {icon: 2, time: 1500, shade: 0.1});
                    }
                    // else if(res.code == 0){
                    //     layer.msg(res.msg,{icon:7,time:1500,shade: 0.1});
                    // }
                }
            });
            layer.close(index);
        })
    },
    //批量禁用
    batchForbidden: function (id, num, url) {
        if (id == '') {
            var msg = '确认禁用全部记录吗?';
        } else {
            var msg = '确认批量禁用记录吗?';
        }
        layer.confirm(msg, {icon: 3, title: '提示'}, function (index) {
            $.ajax({
                url: url,
                type: "post",
                dataType: "json",
                data: "ids=" + id + '&num=' + num,
                success: function (res) {
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1, time: 1500, shade: 0.1}, function () {
                            //重载当前页表格
                            $(".layui-laypage-btn").click();
                        });
                    } else if (res.code == 100) {
                        layer.msg(res.msg, {icon: 2, time: 1500, shade: 0.1});
                    }
                    // else if(res.code == 0){
                    //     layer.msg(res.msg,{icon:7,time:1500,shade: 0.1});
                    // }
                }
            });
            layer.close(index);
        })
    },
    //批量启用
    usingAll: function (id, num, url) {
        if (id == '') {
            var msg = '确认启用全部记录吗?';
        } else {
            var msg = '确认批量启用记录吗?';
        }
        layer.confirm(msg, {icon: 3, title: '提示'}, function (index) {
            $.ajax({
                url: url,
                type: "post",
                dataType: "json",
                data: "ids=" + id + '&num=' + num,
                success: function (res) {
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1, time: 1500, shade: 0.1}, function () {
                            //重载当前页表格
                            $(".layui-laypage-btn").click();
                        });
                    } else if (res.code == 100) {
                        layer.msg(res.msg, {icon: 2, time: 1500, shade: 0.1});
                    }
                    // else if(res.code == 0){
                    //     layer.msg(res.msg,{icon:7,time:1500,shade: 0.1});
                    // }
                }
            });
            layer.close(index);
        })
    },
    //批量启用
    excelAll: function (id, map, url) {
        layer.confirm('确认导出数据吗？', {icon: 3, title: '提示'}, function (index) {
            //加载层
            var loading = layer.msg('正在处理数据，请勿关闭页面...', {icon: 16, shade: 0.4, time: false});
            $.ajax({
                url: url,
                type: "post",
                dataType: "json",
                data: "ids=" + id + '&' + map,
                success: function (res) {
                    if (res.code == 200) {
                        //关闭加载层
                        layer.close(loading);
                        location.href = (res.msg);
                    } else if (res.code == 100) {
                        layer.msg(res.msg, {icon: 2, time: 1500, shade: 0.1});
                    }
                }
            });
            layer.close(index);
        })
    },
    //实时刷新数据
    refresh: function (obj, type, url) {
        $.ajax({
            url: url,
            type: 'post',
            dataType: 'json',
            data: 'type=' + type,
            success: function (res) {
                if (res.code == 200) {
                    $('#' + obj).html(res.msg);
                }
            }
        })
    },
    //快捷修改
    change: function (id, url, field, value) {
        $.ajax({
            url: url,
            type: 'post',
            dateType: 'json',
            data: 'id=' + id + '&field=' + field + '&value=' + value,
            success: function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1, time: 1500, shade: 0.1}, function () {
                        //重载当前页表格
                        $(".layui-laypage-btn").click();
                    });
                } else if (res.code == 100) {
                    layer.msg(res.msg, {icon: 2, time: 1500, shade: 0.1}, function () {
                        //重载当前页表格
                        $(".layui-laypage-btn").click();
                    });
                } else if (res.code == 0) {
                    // layer.msg(res.msg,{icon:7,time:1500,shade: 0.1},function(){
                    //重载当前页表格
                    $(".layui-laypage-btn").click();
                    // });
                }
            }
        });
    },
    //上传图片
    upload: function (url, size) {
        var $list = $('#fileList');
        //上传图片,初始化WebUploader
        var uploader = WebUploader.create({

            auto: true,// 选完文件后，是否自动上传。
            swf: '/static/admin/webupload/Uploader.swf',// swf文件路径
            server: url,// 文件接收服务端。
            duplicate: true,// 重复上传图片，true为可重复false为不可重复
            pick: {
                id: "#imgPicker",// 选择文件的按钮
                multiple: false,//true多图上传 false单图上传
                label: "选择图片"
            },
            // fileNumLimit: 1,//限制上传个数
            // fileSizeLimit: 100* 1024 * 1024,// 总上传限制大小
            fileSingleSizeLimit: size * 1024 * 1024, //限制上传单张图片文件大小，单位是B，1M=1024000B
            accept: {
                title: 'Images',
                extensions: 'jpg,jpeg,bmp,png,xlsx',
                mimeTypes: 'image/jpg,image/jpeg,image/png,.xlsx'
            },

            'onUploadSuccess': function (file, data, response) {
                $("#data_photo").val(data._raw);
                $("#img_data").attr('src', data._raw).show();
            }
        });

        uploader.on('fileQueued', function (file) {
            $list.html('<div id="' + file.id + '" class="item">' +
                '<h4 class="info">' + file.name + '</h4>' +
                '<p class="state">正在上传... <i class="fa fa-refresh fa-pulse"></i></p>' +
                '</div>');
        });

        // 文件上传成功
        uploader.on('uploadSuccess', function (file) {
            $('#' + file.id).find('p.state').text('上传成功！');
        });

        // 文件上传失败，显示上传出错。
        uploader.on('uploadError', function (file) {
            $('#' + file.id).find('p.state').text('上传出错!');
        });

        //错误信息提示
        uploader.on('error', function (code) {
            switch (code) {
                case 'F_EXCEED_SIZE':
                    layer.msg('图片大小不得超过' + uploader.options.fileSingleSizeLimit / 1024 / 1024 + 'MB！', {
                        icon: 2,
                        shade: 0.1,
                        time: 1500
                    });
                    break;
                case 'Q_TYPE_DENIED':
                    layer.msg('无效图片类型，请上传正确的图片格式！', {icon: 2, shade: 0.1, time: 1500});
                    break;
                default:
                    layer.msg('上传错误，请刷新！', {icon: 2, shade: 0.1, time: 1500});
                    break;
            }
        });
    },
    /*跳转页面*/
    href: function (url) {
        $.get(url, {}, function (res) {
            if (res.code != 0) {
                window.location.href = url;
            }
        })
    },
    /*弹出层*/
    layer_show: function (title, url, w, h) {
        if (title == null || title == '') {
            title = "页面";
        }
        ;
        if (url == null || url == '') {
            url = "404.html";
        }
        ;
        if (w == null || w == '') {
            w = ($(window).width() - 300)
        }
        ;
        if (h == null || h == '') {
            h = ($(window).height() - 50);
        }
        ;
        layer.open({
            anim: 0,//css动画 0-6
            isOutAnim: true,
            type: 2,
            // skin:'layui-layer-lan',//皮肤layui-layer-lan layui-layer-molv
            area: [w + 'px', h + 'px'],
            // btn: ['保存', '关闭'],
            fix: false, //不固定
            maxmin: false,//最大最小化
            shadeClose: true,//点击遮罩层关闭
            shade: 0.4,
            title: title,
            content: url,
        });
    },
    layer_show1: function (title, content, w, h) {
        if (w == null || w == '') {
            w = ($(window).width() - 400)
        }
        ;
        if (h == null || h == '') {
            h = ($(window).height() - 50);
        }
        ;
        layer.open({
            type: 1,
            anim: 0,//css动画 0-6
            isOutAnim: true,
            fix: false, //不固定
            maxmin: false,//最大最小化
            shadeClose: true,//点击遮罩层关闭
            area: [w + 'px', h + 'px'],
            title: title,
            // skin: 'layui-layer-demo', //加上边框
            content: content
        });
    },
    /*关闭弹出框口*/
    layer_close: function (type) {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
        if (type != "close") {
            parent.layui.table.reload('testReload', {page: {curr: 1}});
        }
    }
}